const dateUtils = {
  formatDate(date, fmt = 'yyyy-MM-dd') {
    if (!date) return '';
    if (typeof date === 'string') {
      date = new Date(date);
    }

    if (!(date instanceof Date) || isNaN(date.getTime())) {
      return '';
    }

    const map = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
      'q+': Math.floor((date.getMonth() + 3) / 3),
      'S+': date.getMilliseconds()
    };

    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }

    Object.keys(map).forEach((key) => {
      if (new RegExp(`(${key})`).test(fmt)) {
        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? map[key] : ('00' + map[key]).substr(('' + map[key]).length));
      }
    });

    return fmt;
  },

  formatRelativeTime(timestamp) {
    if (!timestamp) return '';

    const now = new Date().getTime();
    const diff = now - timestamp;

    if (diff < 0) {
      return '未来时间';
    }

    const minute = 60 * 1000;
    const hour = 60 * minute;
    const day = 24 * hour;
    const month = 30 * day;
    const year = 365 * day;

    if (diff < minute) {
      return Math.floor(diff / 1000) + '秒前';
    } else if (diff < hour) {
      return Math.floor(diff / minute) + '分钟前';
    } else if (diff < day) {
      return Math.floor(diff / hour) + '小时前';
    } else if (diff < month) {
      return Math.floor(diff / day) + '天前';
    } else if (diff < year) {
      return Math.floor(diff / month) + '个月前';
    } else {
      return Math.floor(diff / year) + '年前';
    }
  },

  getWeekDay(date) {
    if (!date) return '';
    if (typeof date === 'string') {
      date = new Date(date);
    }

    const weekDays = ['日', '一', '二', '三', '四', '五', '六'];
    return weekDays[date.getDay()];
  },

  isToday(date) {
    if (!date) return false;
    if (typeof date === 'string') {
      date = new Date(date);
    }

    const today = new Date();
    return (
      date.getFullYear() === today.getFullYear() &&
      date.getMonth() === today.getMonth() &&
      date.getDate() === today.getDate()
    );
  },

  isSameDay(date1, date2) {
    if (!date1 || !date2) return false;
    if (typeof date1 === 'string') date1 = new Date(date1);
    if (typeof date2 === 'string') date2 = new Date(date2);

    return (
      date1.getFullYear() === date2.getFullYear() &&
      date1.getMonth() === date2.getMonth() &&
      date1.getDate() === date2.getDate()
    );
  },

  addDays(date, days) {
    if (!date) return null;
    if (typeof date === 'string') {
      date = new Date(date);
    }

    const newDate = new Date(date);
    newDate.setDate(newDate.getDate() + days);
    return newDate;
  },

  addMonths(date, months) {
    if (!date) return null;
    if (typeof date === 'string') {
      date = new Date(date);
    }

    const newDate = new Date(date);
    newDate.setMonth(newDate.getMonth() + months);
    return newDate;
  },

  addYears(date, years) {
    if (!date) return null;
    if (typeof date === 'string') {
      date = new Date(date);
    }

    const newDate = new Date(date);
    newDate.setFullYear(newDate.getFullYear() + years);
    return newDate;
  }
};

export default dateUtils;